﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataCentral.UsecaseControllers;
using DataCentral.Forms;
using CommonData;

namespace DataCentral.UserControls
{
    public partial class AssignSalePersonControl : DataCentral.CommonCenterControl
    {
        private AssignSalePersonUsecase _UCAssignSalePerson;
        private bool _ISLoading = false;
        public AssignSalePersonControl()
        {
            InitializeComponent();
            _UCAssignSalePerson = new AssignSalePersonUsecase();
        }

        private void AssignSalePerson_Load(object sender, EventArgs e)
        {
            _ISLoading = true;
            _UCAssignSalePerson.CreateSaleTeamList(imlSaleTeam);
            _ISLoading = false;
            imlSaleTeam_SelectedIndexChanged(null, null);
        }

        private void imlSaleTeam_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!_ISLoading && imlSaleTeam.SelectedItem != null)
            {
                string strSaleTeam = imlSaleTeam.SelectedItem.ToString();
                decimal saleTeamID = Convert.ToDecimal(strSaleTeam.Substring(0, strSaleTeam.IndexOf('.')));
                _UCAssignSalePerson.CreateSysSlsPersonList(saleTeamID, imlSysPerson, imlTeamPerson);

                grpTeamPerson.Text = string.Format("Nhân viên của đội bán <{0}>", imlSaleTeam.SelectedItem.ToString());    
            }
        }


        private void imlSysPerson_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnAdd.Enabled = imlSysPerson.SelectedIndex >= 0;
        }

        private void imlTeamPerson_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = imlTeamPerson.SelectedIndex >= 0;
            btnEdit.Enabled = btnAdd.Enabled;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string strFull = imlTeamPerson.SelectedItem.ToString();
            SalePersonInfo fiSalePerson = new SalePersonInfo(strFull,0);
            EditSalePersonForm frmEditSalePerson = new EditSalePersonForm(fiSalePerson);
            if (frmEditSalePerson.ShowDialog() == DialogResult.OK)
            {
                fiSalePerson = frmEditSalePerson.IFSalePerson;
                imlTeamPerson.SetItemValue(string.Format("{0}.<{1}> {2}-{3}", fiSalePerson.SalePersonID, fiSalePerson.SalePerTye, fiSalePerson.SalePersonCode, fiSalePerson.SalePersonName), imlTeamPerson.SelectedIndex);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (imlSysPerson.SelectedIndex >= 0)
            {
                string strFull = imlSysPerson.SelectedItem.ToString();
                SalePersonInfo fiSalePerson = new SalePersonInfo(strFull,Convert.ToDecimal(imlSaleTeam.SelectedItem.ToString().Substring(0,imlSaleTeam.SelectedItem.ToString().IndexOf('.'))));
                CommonDataAccess cmDataAccess = new CommonDataAccess();
                List<ParameterObject> lstParams = new List<ParameterObject>();
                lstParams.Add(new ParameterObject("@SalePersonID", fiSalePerson.SalePersonID));
                lstParams.Add(new ParameterObject("@SlsperType", fiSalePerson.SalePerTye));
                lstParams.Add(new ParameterObject("@SlsperCode", fiSalePerson.SalePersonCode));
                lstParams.Add(new ParameterObject("@Name", fiSalePerson.SalePersonName));
                lstParams.Add(new ParameterObject("@SlsTeamID", imlSaleTeam.SelectedItem.ToString().Substring(0,imlSaleTeam.SelectedItem.ToString().IndexOf('.'))));
                cmDataAccess.ExecuteSprocNonQuery("DATA_InsertUpdateSalePerson", lstParams);
                imlTeamPerson.Items.Add(imlSysPerson.SelectedItem, fiSalePerson.SalePerTye == "NVBH" ? 0 : 1);
                imlSysPerson.Items.RemoveAt(imlSysPerson.SelectedIndex);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (imlTeamPerson.SelectedIndex >= 0)
            {
                string strFull = imlTeamPerson.SelectedItem.ToString();
                SalePersonInfo fiSalePerson = new SalePersonInfo(strFull,0);
                CommonDataAccess cmDataAccess = new CommonDataAccess();
                List<ParameterObject> lstParams = new List<ParameterObject>();
                lstParams.Add(new ParameterObject("@SalePersonID", fiSalePerson.SalePersonID));
                lstParams.Add(new ParameterObject("@SlsperType", fiSalePerson.SalePerTye));
                lstParams.Add(new ParameterObject("@SlsperCode", fiSalePerson.SalePersonCode));
                lstParams.Add(new ParameterObject("@Name", fiSalePerson.SalePersonName));
                lstParams.Add(new ParameterObject("@SlsTeamID", 0));
                cmDataAccess.ExecuteSprocNonQuery("DATA_InsertUpdateSalePerson", lstParams);

                imlSysPerson.Items.Add(imlTeamPerson.SelectedItem, fiSalePerson.SalePerTye == "NVBH" ? 0 : 1);
                imlTeamPerson.Items.RemoveAt(imlTeamPerson.SelectedIndex);
            }
        }

        private void imlSysPerson_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (btnAdd.Enabled) btnAdd_Click(sender, e);
        }

        private void imlTeamPerson_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (btnDelete.Enabled) btnDelete_Click(sender, e);
        }
    }
}
